﻿using System.Collections.Generic;
using FairyGUI;

public class FCardTypePopup : GComponent
{
    GList _list;
    List<int> _selectedList;
    List<string> _dataList;
    ZeroDeletegate _callBack;

    public override void ConstructFromXML(FairyGUI.Utils.XML cxml)
    {
        base.ConstructFromXML(cxml);

        _list = this.GetChild("list").asList;
        _list.itemRenderer = RenderItem;
        _list.onClickItem.Add(OnCardTypeListClick);
        _selectedList = new List<int>();
    }

    public void InitData(List<string>dataList,ZeroDeletegate cb)
    {
        _callBack = cb;
        _dataList = dataList;
        _list.RemoveChildrenToPool();
        _list.numItems = dataList.Count;
    }

    private void RenderItem(int index,GObject item)
    {
        item.text = _dataList[index];
    }

    private void OnCardTypeListClick(EventContext context)
    {
        var index = _list.GetChildIndex((GObject)context.data);
        if (_selectedList.Contains(index)) _selectedList.Remove(index);
        else _selectedList.Add(index);

        if (_callBack != null) _callBack();
    }

    public List<int> GetSelectedList()
    {
        return _selectedList;
    }
}

